import { Input, Message, Modal } from '@arco-design/web-react';
import { ReactNode } from 'react';
import DetailItem from './detailItem';

interface Props {
  title?: string;
  content?: ReactNode;
  okText?: string;
  cancelText?: string;
  type?: 'info' | 'warning' | 'success' | 'error';
}
// 通用确认取消弹窗
export function confirmDialog({ title, content, okText, cancelText, type = 'info' }: Props) {
  return new Promise((resolve) => {
    Modal.confirm({
      title: title ?? '提示',
      content: content ?? '',
      okText: okText ?? '确定',
      cancelText: cancelText ?? '取消',
      simple: false,
      onCancel: () => resolve(0),
      onOk: () => resolve(1),
    });
  });
}

// 输入框弹窗
export function inputDialog(value?: string, title?: string, required?: boolean) {
  return new Promise<string | number>((resolve) => {
    let _value = '';
    function handleConfirm() {
      if (required && !_value) {
        Message.warning('请输入' + title);
        resolve(0);
      } else {
        resolve(_value);
      }
    }
    let _title = title ?? '内容';
    Modal.info({
      title: _title,
      content: (
        <div>
          <DetailItem required={required} label={_title} labelSpan={6} outlet={4}>
            <Input defaultValue={value} placeholder={'请输入' + _title} onChange={(v) => (_value = v)} />
          </DetailItem>
        </div>
      ),
      okText: '确定',
      cancelText: '取消',
      escToExit: true,
      simple: false,
      onCancel: () => resolve(0),
      onOk: handleConfirm,
    });
  });
}

// 重置用户密码弹窗
export function resetUserPwdDialog(name: string) {
  return new Promise((resolve) => {
    let pwd = '';
    function handleConfirm() {
      let reg = /^[0-9a-zA-Z]{6,20}$/;
      if (!pwd) {
        resolve(0);
      } else if (!reg.test(pwd)) {
        Message.warning('新密码只允许字母和数字组合,最少6位,最多20位');
      } else {
        resolve(pwd);
      }
    }
    Modal.warning({
      title: '重置密码',
      content: (
        <div>
          <div className="mb-20">请输入"{name}"的新密码</div>
          <Input placeholder="请输入密码" onChange={(v) => (pwd = v)} />
        </div>
      ),
      okText: '确定',
      cancelText: '取消',
      simple: false,
      onCancel: () => resolve(0),
      onOk: handleConfirm,
    });
  });
}
